//package com.oauth.resource.config.feign;
//
//import cn.hutool.core.collection.CollectionUtil;
//import cn.hutool.core.lang.TypeReference;
//import cn.hutool.core.util.ArrayUtil;
//import cn.hutool.core.util.EscapeUtil;
//import cn.hutool.json.JSONUtil;
//import com.oauth.common.constant.RequestHeaderConstant;
//import com.secure.annotation.ApiSecure;
//import com.secure.bean.SecureRequest;
//import com.secure.constant.KeyConstant;
//import com.secure.constant.RequestConstant;
//import com.secure.util.RequestUtil;
//import com.secure.util.RsaUtil;
//import feign.RequestInterceptor;
//import feign.RequestTemplate;
//import org.springframework.http.MediaType;
//import org.springframework.util.MimeTypeUtils;
//
//import java.io.UnsupportedEncodingException;
//import java.lang.reflect.Method;
//import java.net.URLDecoder;
//import java.net.URLEncoder;
//import java.nio.charset.StandardCharsets;
//import java.time.Instant;
//import java.util.*;
//
///**
// * feign请求拦截器
// */
//public class FeignRequestInterceptor implements RequestInterceptor {
//
//    private boolean isNeedApiSecureHandle(RequestTemplate requestTemplate){
//        Method method = requestTemplate.methodMetadata().method();
//        return method.isAnnotationPresent(ApiSecure.class);
//    }
//
//    @Override
//    public void apply(RequestTemplate requestTemplate) {
//        //application-form请求补充boundary
//        Collection<String> contentTypes = requestTemplate.headers().get(RequestConstant.CONTENT_TYPE);
//        if (CollectionUtil.isNotEmpty(contentTypes) && contentTypes.stream().anyMatch(contentType -> contentType.contains(MediaType.MULTIPART_FORM_DATA_VALUE))) {
//            requestTemplate.header(RequestConstant.CONTENT_TYPE, RequestUtil.getFormDataContentTypeWithBoundary());
//        }
//        String contentType = contentTypes.iterator().next();
//
////        if(true){
////            return;
////        }
//
//        SecureRequest secureRequest = new SecureRequest();
//        secureRequest.setAppId("user1");
//        secureRequest.setTimestamp(Instant.now().getEpochSecond());
//        byte[] body = requestTemplate.body();
//        Map<String, Collection<String>> queries = requestTemplate.queries();
//        if(ArrayUtil.isNotEmpty(body)){
//            String plainBody = new String(body,StandardCharsets.UTF_8);
//            TreeMap<String, String> plainMap;
//            if(JSONUtil.isTypeJSONObject(plainBody)){
//                plainMap = JSONUtil.toBean(plainBody, new TypeReference<TreeMap<String, String>>() {
//                }, true);
//                String sign = RsaUtil.sign(plainMap, KeyConstant.RSA_DEFAULT_PRIVATE_KEY);
//                plainMap.put("signature",sign);
//                String encryptText = RsaUtil.encryptByPublicKey(plainMap, KeyConstant.RSA_DEFAULT_PUBLIC_KEY);
//                secureRequest.setData(encryptText);
//            }else{
//                plainMap = new TreeMap<>();
//                plainMap.put("_stringBody",plainBody);
//                String sign = RsaUtil.sign(plainMap, KeyConstant.RSA_DEFAULT_PRIVATE_KEY);
//                plainMap.put("signature",sign);
//                String encryptText = RsaUtil.encryptByPublicKey(plainMap, KeyConstant.RSA_DEFAULT_PUBLIC_KEY);
//                if(contentType.contains(MediaType.APPLICATION_FORM_URLENCODED_VALUE)) {
//                    try {
//                        encryptText = URLEncoder.encode(encryptText, StandardCharsets.UTF_8.toString());
//                    } catch (UnsupportedEncodingException e) {
//                        throw new RuntimeException(e);
//                    }
//                }
//                secureRequest.setData(encryptText);
//            }
//            requestTemplate.body(JSONUtil.toJsonStr(secureRequest));
//        }else if(CollectionUtil.isNotEmpty(queries)){
//            TreeMap<String, String> plainMap = new TreeMap<>();
//            queries.forEach((parameterName,parameterValues)->{
//                String decodeText;
//                try {
//                    decodeText = URLDecoder.decode(parameterValues.iterator().next(), StandardCharsets.UTF_8.toString());
//                } catch (UnsupportedEncodingException e) {
//                    throw new RuntimeException(e);
//                }
//                plainMap.put(parameterName,decodeText);
//            });
//            String sign = RsaUtil.sign(plainMap, KeyConstant.RSA_DEFAULT_PRIVATE_KEY);
//            plainMap.put("signature",sign);
//            String encryptText = RsaUtil.encryptByPublicKey(plainMap, KeyConstant.RSA_DEFAULT_PUBLIC_KEY);
//            String encodeText;
//            try {
//                encodeText = URLEncoder.encode(encryptText, StandardCharsets.UTF_8.toString());
//            } catch (UnsupportedEncodingException e) {
//                throw new RuntimeException(e);
//            }
//            secureRequest.setData(encodeText);
//            Map<String, Collection<String>> parameters = new HashMap<>();
//            parameters.put(RequestConstant.APPID,Collections.singletonList(secureRequest.getAppId()));
//            parameters.put(RequestConstant.TIMESTAMP,Collections.singletonList(secureRequest.getTimestamp().toString()));
//            parameters.put(RequestConstant.DATA,Collections.singletonList(secureRequest.getData()));
//            requestTemplate.queries(parameters);
//        }
////        if(CollectionUtil.isNotEmpty(contentTypes) && RequestUtil.isBodyRawContentTpe(contentTypes.iterator().next())){
////            byte[] body = requestTemplate.body();
////            String plainBody = new String(body,StandardCharsets.UTF_8);
////            if(JSONUtil.isTypeJSONObject(plainBody)){
////                TreeMap<String, String> plainMap = JSONUtil.toBean(plainBody, new TypeReference<TreeMap<String, String>>() {
////                }, true);
////                String sign = RsaUtil.sign(plainMap, KeyConstant.RSA_DEFAULT_PRIVATE_KEY);
////                plainMap.put("signature",sign);
////                String encryptText = RsaUtil.encryptByPublicKey(plainMap, KeyConstant.RSA_DEFAULT_PUBLIC_KEY);
////                secureRequest.setData(encryptText);
////            }else{
////
////            }
////            requestTemplate.body(JSONUtil.toJsonStr(secureRequest));
////        }else{
////            Map<String, Collection<String>> queries = requestTemplate.queries();
////            TreeMap<String, String> plainMap = new TreeMap<>();
////            queries.forEach((parameterName,parameterValues)->{
////                String decodeText;
////                try {
////                    decodeText = URLDecoder.decode(parameterValues.iterator().next(), StandardCharsets.UTF_8.toString());
////                } catch (UnsupportedEncodingException e) {
////                    throw new RuntimeException(e);
////                }
////                plainMap.put(parameterName,decodeText);
////            });
////            String sign = RsaUtil.sign(plainMap, KeyConstant.RSA_DEFAULT_PRIVATE_KEY);
////            plainMap.put("signature",sign);
////            String encryptText = RsaUtil.encryptByPublicKey(plainMap, KeyConstant.RSA_DEFAULT_PUBLIC_KEY);
////            String encodeText;
////            try {
////                encodeText = URLEncoder.encode(encryptText, StandardCharsets.UTF_8.toString());
////            } catch (UnsupportedEncodingException e) {
////                throw new RuntimeException(e);
////            }
////            secureRequest.setData(encodeText);
////            Map<String, Collection<String>> parameters = new HashMap<>();
////            parameters.put(RequestConstant.APPID,Collections.singletonList(secureRequest.getAppId()));
////            parameters.put(RequestConstant.TIMESTAMP,Collections.singletonList(secureRequest.getTimestamp().toString()));
////            parameters.put(RequestConstant.DATA,Collections.singletonList(secureRequest.getData()));
////            requestTemplate.queries(parameters);
////        }
//
//    }
//}
